package com.bifrost.admin.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bifrost.common.model.ResultModel;
import com.bifrost.exception.ExternalOrderProcessException;
import com.bifrost.exception.KnownRuntimeException;
import com.bifrost.exception.OriginOrderNotFundException;
import com.bifrost.service.BonusPointsService;
import com.bifrost.service.MemberService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 * @author rangf
 * @date 2018/3/24
 */
@RestController
@RequestMapping("/admin/member")
public class MemberController {

    @Autowired
    MemberService memberService;
    @Autowired
    BonusPointsService bonusPointsService;

    private static final Logger logger = LoggerFactory.getLogger(MemberController.class);

    @GetMapping("/score")
    public ResultModel getReturnScore(@RequestParam Long returnId ){
        try {
            String result = memberService.getReturnScoreForAdmin(returnId);
            JSONObject object = JSON.parseObject(result);
            String errCode = object.getString("errCode");
            String errMessage = object.getString("errMessage");
            String data = object.getString("data");
            if (StringUtils.isEmpty(result)) {
                return ResultModel.error("请求重百退货积分接口失败");
            }

            ResultModel defaultResult = ResultModel.ok()
                    .put("errCode", StringUtils.isEmpty(errCode) ? "" : errCode.trim())
                    .put("errMessage", StringUtils.isEmpty(errMessage) ? "" : errMessage.trim())
                    .put("data", StringUtils.isEmpty(data) ? "" : data.trim());

            // crm 积分不足
            if(!"Y".equalsIgnoreCase(StringUtils.trim(errCode))){
                return defaultResult;
            }
            try{
                //校验微商城积分
                Integer resp = bonusPointsService.checkReturn(returnId);
                //积分不足
                if (resp < 0) {
                    return defaultResult
                            .put("errCode", "N")
                            .put("errMessage", String.format("微商城积分不足，差额：%s分",Math.abs(resp)))
                            .put("data", Math.abs(resp));
                }
                return defaultResult;
            } catch (OriginOrderNotFundException e) {
                //未找到原单则直接返回ok
                return ResultModel.ok();
            }catch (ExternalOrderProcessException e){
                //系统未注册的门店原单未积分则直接返回ok
                return ResultModel.ok();
                //返回可预知的校验退单错误信息
            }catch (KnownRuntimeException e){
                return ResultModel.ok()
                        .put("errCode", "N")
                        .put("errMessage", e.getMessage())
                        .put("data", "");
            }

        }catch (Exception e){
            logger.error("cbest member score response json is error");
        }
        return ResultModel.error("请求重百退货积分接口失败");
    }
}